package com.sinodata.bsm.center.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.sinodata.bsm.center.util.system.SystemContext;
import com.sinodata.bsm.common.utils.PropertiesUtil;

/**
 * <p>
 * Description:
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-4-17 下午7:39:21          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class AnalyzePropertiesUtil {

    private static AnalyzePropertiesUtil instance = null;

    private static final Map<String, Map<String, String>> cache = new HashMap<String, Map<String, String>>();

    private static final Logger logger = Logger.getLogger(AnalyzePropertiesUtil.class);

    private static String path = (String) SystemContext.getProperty("AppContextPath");

    public static AnalyzePropertiesUtil getInstance() {
        if (instance == null) {
            instance = new AnalyzePropertiesUtil();
        }
        return instance;
    }

    private AnalyzePropertiesUtil() {

    }

    public Map<String, String> getProperties(String filename) {
        Map<String, String> value = null;
        if (cache.containsKey(filename)) {
            value = cache.get(filename);
        } else {
            value = addFileToCache(filename);
            cache.put(filename, value);
        }
        return value;
    }

    private Map<String, String> addFileToCache(String filename) {
        String fileFullName = path + filename;
        System.out.println(fileFullName);
        Map<String, String> map = new HashMap<String, String>();
        try {
            Properties props = PropertiesUtil.getProperties(fileFullName);
            Enumeration<?> names = props.propertyNames();
            while (names.hasMoreElements()) {
                String key = (String) names.nextElement();
                map.put(key, props.getProperty(key));
            }
        } catch (Exception e) {
            logger.error("通过文件名获得Properties出错", e);
        }
        return map;
    }

    public static void main(String[] args) {
        String filename = "WEB-INF/conf/send-process-impclass.properties";
        AnalyzePropertiesUtil a = AnalyzePropertiesUtil.getInstance();
        Map<String, String> map = a.getProperties(filename);
        for (String s : map.values()) {
            System.out.println(s);
        }
    }
}
